// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2013 - 2018 Intel Corporation. */

#include <linux/module.h>

#include "i40e.h"
#include "i40e_type.h"
#include "i40e_cmd.h"

/* set fw opcode 0x0622: disable or re-enable firmware's link management */
i40e_status i40e_aq_disable_firmware_link_management(struct i40e_hw *hw, bool link_disable)
{
	struct i40e_aq_desc desc = {0};
	i40e_status ret;
	struct i40e_pf *pf = (struct i40e_pf *)hw->back;

	i40e_fill_default_direct_cmd_desc(&desc, i40e_aqc_opc_set_phy_debug);
	if (link_disable)
		desc.params.internal.param0 = 0x10; /* 0x10: disable firmware's link management */
	else
		desc.params.internal.param0 = 0; /* 0: re-enable firmware's link management */

	ret = i40e_asq_send_command(hw, &desc, NULL, 0, NULL);
	if (!ret) {
		dev_info(&pf->pdev->dev, "AQ command sent Status : Success\n");
	} else if (ret == I40E_ERR_ADMIN_QUEUE_ERROR) {
		dev_info(&pf->pdev->dev, "AQ command send failed Opcode %x AQ Error: %d\n",
				desc.opcode, pf->hw.aq.asq_last_status);
	} else {
		dev_info(&pf->pdev->dev, "AQ command send failed Opcode %x Status: %d\n",
				desc.opcode, ret);
	}
	dev_info(&pf->pdev->dev,
			"AQ desc WB 0x%04x 0x%04x 0x%04x 0x%04x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
			desc.flags, desc.opcode, desc.datalen, desc.retval,
			desc.cookie_high, desc.cookie_low,
			desc.params.internal.param0, desc.params.internal.param1,
			desc.params.internal.param2, desc.params.internal.param3);
	return ret;
}
EXPORT_SYMBOL(i40e_aq_disable_firmware_link_management);

i40e_status i40e_get_link_info(struct i40e_hw *hw, struct i40e_aqc_get_link_status *stat)
{
	struct i40e_aq_desc desc = {0};
	struct i40e_aqc_get_link_status *resp = (struct i40e_aqc_get_link_status *)&desc.params.raw;
	i40e_status status;
	struct i40e_pf *pf = (struct i40e_pf *)hw->back;

	i40e_fill_default_direct_cmd_desc(&desc, i40e_aqc_opc_get_link_status);
	resp->command_flags = cpu_to_le16(I40E_AQ_LSE_ENABLE);

	status = i40e_asq_send_command(hw, &desc, NULL, 0, NULL);
	if (status != I40E_SUCCESS) {
		dev_info(&pf->pdev->dev, "get link status error ret(0x%x).\n", status);
		return status;
	}
	memcpy(stat, resp, sizeof(struct i40e_aqc_get_link_status));
	return I40E_SUCCESS;
}
EXPORT_SYMBOL(i40e_get_link_info);

i40e_status i40e_get_nvm_version(struct i40e_hw *hw, unsigned short int *version)
{
	i40e_status status;
	struct i40e_pf *pf = (struct i40e_pf *)hw->back;

	status = i40e_read_nvm_word(hw, I40E_SR_NVM_DEV_STARTER_VERSION, version);
	if (status)
		dev_info(&pf->pdev->dev, "get nvm vesion error ret(0x%x).\n", status);

	return status;
}
EXPORT_SYMBOL(i40e_get_nvm_version);
